﻿#pragma once
#include<map>
#include<iostream>
#include<vector>
using namespace std;
// 枚举值表⽰颜⾊
enum Colour
{
	RED,
	BLACK
};
template<class K,class V>
struct RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;
	RBTreeNode(const pair<K, V>& kv) :_kv(kv), _left(nullptr), _right(nullptr), _parent(nullptr) {}
};


template <class K, class V>
class RBTree
{
	typedef RBTreeNode<K,V> Node;
public:
	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在⿊⾊结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}
		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红⾊结点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}
	void InOrder()
	{
		_inorder(_root);
	}
	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;
		// 参考值
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else if (key < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else
				return cur;
		}
		return nullptr;
	}
	void RoteR(Node* parent)
	{
		Node* prev = parent->_parent;
		Node* sub = parent;
		Node* subl = sub->_left;
		Node* sublr = subl->_right;
		sub->_left = sublr;
		if (sublr) sublr->_parent = sub;
		subl->_right = sub;
		sub->_parent = subl;
		
		if (prev == nullptr)
		{
			_root = subl;
			_root->_parent = nullptr;
		}
		else
		{
			if (prev->_right == parent)
			{
				prev->_right = subl;
				
			}
			else
			{
				prev->_left = subl;
				
			}
			subl->_parent = prev;
		}
	}
	void RoteL(Node* parent)
	{
		Node* prev = parent->_parent;

		Node* sub = parent;
		Node* subr = sub->_right;
		Node* subrl = subr->_left;

		sub->_right = subrl;
		if (subrl) subrl->_parent = sub;
		subr->_left = sub;
		sub->_parent = subr;
		if (prev == nullptr)
		{
			_root = subr;
			_root->_parent = nullptr;
		}
		else
		{
			if (prev->_left == parent)
			{
				prev->_left = subr;
				subr->_parent = prev;
			}
			else
			{
				prev->_right = subr;
				subr->_parent = prev;
			}
		}

	}
	bool Insert(const pair<K, V>& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}
		Node* cur = _root;
		Node* parent = _root;
		while (cur)
		{
			if (data.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (data.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(data);
		cur->_col = RED;
		if (parent->_kv.first > data.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;
		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_left == cur)
					{
						RoteR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RoteL(parent);
						RoteR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_right == cur)
					{
						RoteL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RoteR(parent);
						RoteL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;//防止一直向上处理到parent是空时
		return true;
	}
//bool Insert(const pair<K, V>& kv)
//{
//	if (_root == nullptr)
//	{
//		_root = new Node(kv);
//		_root->_col = BLACK;
//
//		return true;
//	}
//
//	Node* parent = nullptr;
//	Node* cur = _root;
//	while (cur)
//	{
//		if (cur->_kv.first < kv.first)
//		{
//			parent = cur;
//			cur = cur->_right;
//		}
//		else if (cur->_kv.first > kv.first)
//		{
//			parent = cur;
//			cur = cur->_left;
//		}
//		else
//		{
//			return false;
//		}
//	}
//
//	cur = new Node(kv);
//	cur->_col = RED;
//	if (parent->_kv.first < kv.first)
//	{
//		parent->_right = cur;
//	}
//	else
//	{
//		parent->_left = cur;
//	}
//	// 链接父亲
//	cur->_parent = parent;
//
//	// 父亲是红色，出现连续的红色节点，需要处理
//	while (parent && parent->_col == RED)
//	{
//		Node* grandfather = parent->_parent;
//		if (parent == grandfather->_left)
//		{
//			//   g
//			// p   u
//			Node* uncle = grandfather->_right;
//			if (uncle && uncle->_col == RED)
//			{
//				// 变色
//				parent->_col = uncle->_col = BLACK;
//				grandfather->_col = RED;
//
//				// 继续往上处理
//				cur = grandfather;
//				parent = cur->_parent;
//			}
//			else
//			{
//				if (cur == parent->_left)
//				{
//					//     g
//					//   p    u
//					// c
//					RoteR(grandfather);
//					parent->_col = BLACK;
//					grandfather->_col = RED;
//				}
//				else
//				{
//					//      g
//					//   p    u
//					//     c
//					RoteL(parent);
//					RoteR(grandfather);
//					cur->_col = BLACK;
//					grandfather->_col = RED;
//				}
//
//				break;
//			}
//		}
//		else
//		{
//			//   g
//			// u   p
//			Node* uncle = grandfather->_left;
//			// 叔叔存在且为红，-》变色即可
//			if (uncle && uncle->_col == RED)
//			{
//				parent->_col = uncle->_col = BLACK;
//				grandfather->_col = RED;
//
//				// 继续往上处理
//				cur = grandfather;
//				parent = cur->_parent;
//			}
//			else // 叔叔不存在，或者存在且为黑
//			{
//				// 情况二：叔叔不存在或者存在且为黑
//				// 旋转+变色
//				//   g
//				// u   p
//				//       c
//				if (cur == parent->_right)
//				{
//					RoteL(grandfather);
//					parent->_col = BLACK;
//					grandfather->_col = RED;
//				}
//				else
//				{
//					RoteR(parent);
//					RoteL(grandfather);
//					cur->_col = BLACK;
//					grandfather->_col = RED;
//				}
//
//				break;
//			}
//		}
//	}
//
//	_root->_col = BLACK;
//
//	return true;
//}
	int Height()
	{
		 return _height(_root);
	}
	int Size()
	{
		return _size(_root);
	}
private:
	void _inorder(Node* root)
	{
		if (root == nullptr) return;
		_inorder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_inorder(root->_right);
	}
	int _height(Node* root)
	{
		if (root == nullptr) return 0;
		int left = _height(root->_left);
		int right = _height(root->_right);
		return left > right ? left + 1: right + 1;
	}
	int _size(Node* root)
	{
		if (root == nullptr) return 0;
		int left = _size(root->_left);
		int right = _size(root->_right);
		return left + right + 1;
	}
	Node* _root=nullptr;
};
